package com.zhuoan.im.webapp.controller.user;

import com.zhuoan.im.webapp.biz.UserBiz;
import com.zhuoan.im.webapp.controller.BaseController;
import com.zhuoan.im.webapp.controller.server.ServerStartController;
import com.zhuoan.im.webapp.dao.model.User;
import com.zhuoan.im.webapp.dao.model.form.UserForm;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.BindException;

/**
 * LoginController
 *
 * @author weixiang.wu
 * @date 2018-10-12 17:40
 **/
@Controller
@RequestMapping("/")
public class UserController extends BaseController {

    private final static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Resource
    private ServerStartController serverStartController;

    @Resource
    private UserBiz userBiz;

    @RequestMapping("/chat")
    public String chat() {
        return "chat";
    }

    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    @RequestMapping("/contact")
    public String contact() {
        return "contact";
    }

    @RequestMapping("/test")
    public String test(HttpServletRequest request) {
        serverStartController.startServer(request);
        logger.info("IP=[" + getIp(request) + "]启动im服务成功");
        return "test";
    }

    @RequestMapping("userInfoSync")
    public String userInfoSync(UserForm userForm) {
        if (StringUtils.isBlank(userForm.getUsername()) && StringUtils.isBlank(userForm.getBlockAddr())) {
            return "Pass in a username or blockAddr";
        }
        userBiz.addUser(userForm);
        return "SUCCESS";
    }

    @RequestMapping("userInfoUpdate")
    public String userInfoUpdate(UserForm userForm) {
        if (StringUtils.isBlank(userForm.getUsername()) && StringUtils.isBlank(userForm.getBlockAddr())) {
            return "Pass in a username or blockAddr";
        }
        userBiz.updateUserInfoByBlockAddr(userForm);
        return "SUCCESS";
    }

    @RequestMapping("/userA")
    public String userA(HttpServletRequest request) {
        return "userA";
    }

    @RequestMapping("/userB")
    public String userB(HttpServletRequest request) {
        return "userB";
    }
}
